package org.dromara.ai.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.ai.domain.bo.AiCommentBo;
import org.dromara.ai.domain.vo.AiCommentVo;
import org.dromara.ai.domain.AiComment;
import org.dromara.ai.mapper.AiCommentMapper;
import org.dromara.ai.service.IAiCommentService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 评论Service业务层处理
 *
 * @author Tang
 * @date 2024-12-08
 */
@RequiredArgsConstructor
@Service
public class AiCommentServiceImpl implements IAiCommentService {

    private final AiCommentMapper baseMapper;

    /**
     * 查询评论
     *
     * @param commentId 主键
     * @return 评论
     */
    @Override
    public AiCommentVo queryById(Long commentId){
        return baseMapper.selectVoById(commentId);
    }


    /**
     * 查询符合条件的评论列表
     *
     * @param bo 查询条件
     * @return 评论列表
     */
    @Override
    public List<AiCommentVo> queryList(AiCommentBo bo) {
        LambdaQueryWrapper<AiComment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AiComment> buildQueryWrapper(AiCommentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AiComment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, AiComment::getUserId, bo.getUserId());
        lqw.eq(bo.getWorkId() != null, AiComment::getWorkId, bo.getWorkId());
        lqw.like(StringUtils.isNotBlank(bo.getContent()), AiComment::getContent, bo.getContent());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            AiComment::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增评论
     *
     * @param bo 评论
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AiCommentBo bo) {
        AiComment add = MapstructUtils.convert(bo, AiComment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCommentId(add.getCommentId());
        }
        return flag;
    }

    /**
     * 修改评论
     *
     * @param bo 评论
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AiCommentBo bo) {
        AiComment update = MapstructUtils.convert(bo, AiComment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AiComment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除评论信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
